package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {


    Long InsertOrder(Orders orders);

    void addDetail(OrderDetail orderDetail);

    void pay(Orders orders);

    @Select("select estimated_delivery_time from orders where number=#{number}")
    String getByNumber(String number);

    Long CountOrders(Long start, OrdersPageQueryDTO ordersPageQueryDTO);

    List<Orders> getOrders(Long start, OrdersPageQueryDTO ordersPageQueryDTO);

    List<OrderDetail> getOrderDetail(Long id);

    @Select("select *from orders where id=#{id}")
    OrderVO getOrdersById(Long id);

    /**
     * 分页条件查询并按下单时间排序
     *
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单id查询订单明细
     *
     * @param orderId
     * @return
     */
    @Select("select * from order_detail where order_id = #{orderId}")
    List<OrderDetail> getByOrderId(Long orderId);

    @Update("update orders set status=#{status},cancel_time=#{now} where id =#{id}")
    void cancel(Integer status, Integer id, LocalDateTime now);

    @Delete("delete from order_detail where order_id=#{id}")
    void cancelDetail(Integer id);

    @Select("select count(*) from orders where status=#{toBeConfirmed}")
    Integer CountToBeConfirmed(Integer toBeConfirmed);

    @Select("select count(*) from orders where status=#{confirmed}")
    Integer CountConfirmed(Integer confirmed);

    @Select("select count(*) from orders where status=#{deliveryInProgress}")
    Integer CountDeliveryInProgress(Integer deliveryInProgress);

    @Update("update orders set status=#{status} where id =#{id}")
    void confirm(Integer status, Long id);

    @Update("update orders set status=#{status},cancel_reason=#{cancelReason},cancel_time=#{cancelTime} where id =#{id}")
    void AdminCancel(OrdersCancelDTO ordersCancelDTO);

    @Update("update orders set status=#{status},rejection_reason=#{rejectionReason},cancel_time=#{cancelTime} where id =#{id}")
    void OrdersRejection(OrdersRejectionDTO ordersRejectionDTO);

    @Update("update orders set status=#{status} where id =#{id}")
    void delivery(Integer status, Integer id);

    @Update("update orders set status=#{status},delivery_time=#{now} where id =#{id}")
    void complete(Integer id, Integer status, LocalDateTime now);

    @Select("select *from orders where status=1 and ABS(TIMESTAMPDIFF(MINUTE, #{nowTime}, order_time)) >= 15 ")
    List<Orders> getByOrderTime(LocalDateTime nowTime);

    @Update("update orders set status = 6 ,orders.cancel_time=#{cancelTime} where id=#{id}")
    void ChangeTimeOutStatus(Orders orders);

    @Select("select *from orders where status=#{deliveryInProgress}")
    List<Orders> getBystatus(Integer deliveryInProgress);

    @Update("update orders set status = 5,delivery_time=#{deliveryTime} where id=#{id}")
    void ChangeDeliveringTimeOutStatus(Orders orders);

    @Select("select id from orders where number=#{number}")
    Long getByNumberToId(Orders orders);

    @Select("select number from orders where id =#{id}")
    String getByIdToNumber(Orders orders);

    BigDecimal getSumAmount(LocalDateTime beginTime, LocalDateTime endTime, Integer status);

    @Select("select count(*) from user where create_time < #{endTime}")
    Long SumUser(LocalDateTime endTime);

    @Select("select count(*) from user where create_time < #{endTime} and create_time >#{beginTime}")
    Long CountUser(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where order_time>#{beginTime} and order_time <#{endTime}")
    Long GetOrderCount(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders where order_time>#{beginTime} and order_time <#{endTime} and status= 5")
    Long GetYesOrders(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders")
    Integer gettotal();

    @Select("select count(*) from orders where status = 5")
    Integer getValidOrder();

    List<GoodsSalesDTO> top(LocalDateTime beginTime, LocalDateTime endTime);


    Integer countByMap(Map map);
}
